<?php
include("../class/mysql.class.php");
include("../submit/Articles/get_list_article.php");

$obj = new Mysql ();
// hedhia stat 2


  
//echo "<br>Fournisseur : ".$_POST["id_fournisseur"]."<br>";       
/*echo "<br> UNITE : ".$_POST["id_unite"]."<br>";       
echo "<br> UNITE DE STOCKAGE : ".$_POST["id_unite_stockage"]."<br>";       
echo "<br> TYPE : ".$_POST["id_type"]."<br>";       
echo "<br> LIEU : ".$_POST["id_lieu"]."<br>";*/       

  //$rq  = "select e.id_entree,a.designation,a.ref_article,a.photo_name,e.qte,e.id_fournisseur,e.prix,e.date_add,e.ajouter_par 
  //from st_attr_entree e,st_article a where a.id_article = e.id_article AND e.qte <> 0 ORDER BY e.date_add DESC";
  //echo $rq;
  

  // article selectionner / aucun autre parametres
  if($_POST["id_article"]!=null && $_POST['dateD']==null && $_POST['dateF']==null){
    
    $rq = "select 
    e.id_entree,
    a.designation,
    a.ref_article,
    a.photo_name,
    e.qte,
    e.id_fournisseur,
    e.prix,
    e.date_add,
    e.ajouter_par
    ";
    if(!empty($_POST["id_unite"])){
      $rq .= " ,un.id_unite ";
    }    
    if(!empty($_POST["id_unite"])){
      $rq .= " ,un.id_unite ";
    }    
    if(!empty($_POST["id_unite_stockage"])){
      $rq .= " ,u.id_unite_stockage ";
    }    
    if(!empty($_POST["id_type"])){
      $rq .= " ,t.id_type ";
    }    
    if(!empty($_POST["id_lieu"])){
      $rq .= " ,l.id_lieu ";
    }    
    
    $rq .="
    from st_attr_entree e,
     st_article a ";
    if(!empty($_POST["id_unite"])){
      $rq .= " ,st_att_unite un ";
    }    
    if(!empty($_POST["id_unite_stockage"])){
      $rq .= " ,st_att_unite_stockage u ";
    }    
    if(!empty($_POST["id_type"])){
      $rq .= " ,st_att_type_article t ";
    }    
    if(!empty($_POST["id_lieu"])){
      $rq .= " ,st_att_lieu_article l ";
    }    
     
    $rq .= "where e.id_article=".$_POST['id_article'];
       
    if(!empty($_POST["id_fournisseur"])){
      $rq .=" AND e.id_fournisseur = ".$_POST["id_fournisseur"];
      //echo "<br>Fournisseur : ".$_POST["id_fournisseur"]."<br>";       
    }

    if(!empty($_POST["id_unite"])){
      $rq .=" AND a.id_type = e.id_article";       
      //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
    }

    if(!empty($_POST["id_unite_stockage"])){
      $rq .=" AND a.id_unite_stockage = u.id_unite_stockage AND a.id_unite_stockage = ".$_POST["id_unite_stockage"];       
      //echo "<br> UNITE DE STOCKAGE".$_POST["id_unite_stockage"]."<br>";       
    }

    if(!empty($_POST["id_type"])){
      $rq .=" AND t.id_type = a.id_type AND a.id_type = ".$_POST["id_type"];       
      //echo "<br> TYPE".$_POST["id_type"]."<br>";       
    }

    if(!empty($_POST["id_lieu"])){
      $rq .=" AND l.id_lieu = a.id_lieu AND a.id_lieu = ".$_POST["id_lieu"];       
      //echo "<br> LIEU ".$_POST["id_lieu"]."<br>";       
    }

    $rq .=" AND a.id_article = e.id_article 
    AND e.qte <> 0  ORDER BY e.date_add DESC";
    //echo "article isset<br>".$rq;  
  }
  


  //parametre : date debut / date fin 
  if($_POST['dateD']!=null && $_POST['dateF']!=null && $_POST["id_article"]==null){
    
    $rq = "select e.id_entree,a.designation,a.ref_article,a.photo_name,
             e.qte,e.id_fournisseur,e.prix,e.date_add,e.ajouter_par 
           from st_attr_entree e,st_article a 
           where 
           a.id_article = e.id_article ";
    if(!empty($_POST["id_fournisseur"])){
      $rq .=" AND e.id_fournisseur = ".$_POST["id_fournisseur"];
      //echo "<br>Fournisseur : ".$_POST["id_fournisseur"]."<br>";       
    }       
    $rq .=" AND e.date_add >= '".$_POST['dateD']."' 
           AND e.date_add <= '".$_POST['dateF']."' 
           AND e.qte <> 0 
           ORDER BY e.date_add DESC";
    //echo "date isset<br>".$rq; 
  }
  

  // aucun prametre
  if($_POST['dateD']==null && $_POST['dateF']==null && $_POST["id_article"]==null){
    
    $rq = "select e.id_entree,a.designation,a.ref_article,a.photo_name,
             e.qte,e.id_fournisseur,e.prix,e.date_add,e.ajouter_par 
           from st_attr_entree e,st_article a 
           where 
           a.id_article = e.id_article ";
    if(!empty($_POST["id_fournisseur"])){
      $rq .=" AND e.id_fournisseur = ".$_POST["id_fournisseur"];
      //echo "<br>Fournisseur : ".$_POST["id_fournisseur"]."<br>";       
    }       
    $rq .=" AND e.qte <> 0 
           ORDER BY e.date_add DESC";
    //echo "date isset<br>".$rq; 
  }


  // tout les parametres son saisi
  if($_POST['dateD']!=null && $_POST['dateF']!=null && $_POST["id_article"]!=null){
    
    //$rq = "select e.id_entree,a.designation,a.ref_article,a.photo_name,e.qte,e.id_fournisseur,e.prix,e.date_add,e.ajouter_par from st_attr_entree e,st_article a where a.id_article = e.id_article AND e.id_article=".$_POST['id_article']." AND e.date_add >= '".$_POST['dateD']."' AND e.date_add <= '".$_POST['dateF']."' AND e.qte <> 0 ORDER BY e.date_add DESC";
    //echo "date isset & article<br>".$rq; 
    $rq = "select 
    e.id_entree,
    a.designation,
    a.ref_article,
    a.photo_name,
    e.qte,
    e.id_fournisseur,
    e.prix,
    e.date_add,
    e.ajouter_par
    ";
    
    if(!empty($_POST["id_unite"])){
      $rq .= " ,un.id_unite ";
    }    
    if(!empty($_POST["id_unite_stockage"])){
      $rq .= " ,u.id_unite_stockage ";
    }    
    if(!empty($_POST["id_type"])){
      $rq .= " ,t.id_type ";
    }    
    if(!empty($_POST["id_lieu"])){
      $rq .= " ,l.id_lieu ";
    }    
    
    $rq .="
    from st_attr_entree e,
     st_article a ";
     if(!empty($_POST["id_fournisseur"])){
      $rq .= " ,st_att_fournisseur f ";
    }    
    if(!empty($_POST["id_unite"])){
      $rq .= " ,st_att_unite un ";
    }    
    if(!empty($_POST["id_unite_stockage"])){
      $rq .= " ,st_att_unite_stockage u ";
    }    
    if(!empty($_POST["id_type"])){
      $rq .= " ,st_att_type_article t ";
    }    
    if(!empty($_POST["id_lieu"])){
      $rq .= " ,st_att_lieu_article l ";
    }    
     
    $rq .= "where e.id_article=".$_POST['id_article'];
       

    if(!empty($_POST["id_fournisseur"])){
      $rq .=" AND e.id_fournisseur = ".$_POST["id_fournisseur"]." AND e.id_fournisseur = f.id_fournisseur";
      //echo "<br>Fournisseur : ".$_POST["id_fournisseur"]."<br>";       
    }

    if(!empty($_POST["id_unite"])){
      $rq .=" AND e.id_unite = ".$_POST["id_unite"];       
      //echo "<br> UNITE".$_POST["id_unite"]."<br>";       
    }

    if(!empty($_POST["id_unite_stockage"])){
      $rq .=" AND a.id_unite_stockage = u.id_unite_stockage AND a.id_unite_stockage = ".$_POST["id_unite_stockage"];       
      //echo "<br> UNITE DE STOCKAGE".$_POST["id_unite_stockage"]."<br>";       
    }

    if(!empty($_POST["id_type"])){
      $rq .=" AND t.id_type = a.id_type AND a.id_type = ".$_POST["id_type"];       
      //echo "<br> TYPE".$_POST["id_type"]."<br>";       
    }

    if(!empty($_POST["id_lieu"])){
      $rq .=" AND l.id_lieu = a.id_lieu AND a.id_lieu = ".$_POST["id_lieu"];       
      //echo "<br> LIEU ".$_POST["id_lieu"]."<br>";       
    }

    $rq .=" AND a.id_article = e.id_article 
    AND e.date_add >= '".$_POST['dateD']."' AND e.date_add <= '".$_POST['dateF']."' 
    AND e.qte <> 0  ORDER BY e.date_add DESC";
    //echo "article isset<br>".$rq;  
  }
  //echo $rq;
  $rs = mysql_query($rq);
  ?>
	<div id="printablediv">
  <div id="container">

 <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
    <thead>
    <tr>
        <th style="text-align:left;width:50px;">#</th> 
        <th style="text-align:left;width:150px;">Référence</th>     
        <th style="text-align:left;width:350px;">Designation</th>      
        <th style="text-align:left;width:150px;">Qte</th>
        <th style="text-align:left;width:150px;">Fournisseur</th>
        <th style="text-align:left;width:150px;">Prix Achat</th>
        <th style="text-align:left;width:150px;">Opération</th>
    </tr>
</thead>
<tbody>
    
    <?php  
    $cpt = 1;
    $qt_Total = 0;
    while($row = mysql_fetch_object($rs))
    {
      
    ?>
    
    <tr>
        <td style="text-align:left;width:50px;"><?php echo $cpt?></td>
        <td style="text-align:left;width:150px;"><?php echo '<a class="fancybox-effects-a" href="'.$row->photo_name.'">'.$row->ref_article.'</a>' ?></td>
        <td style="text-align:left;width:350px;"><?php echo $row->designation?></td>
        <td style="text-align:left;width:150px;"><?php echo number_format($row->qte, 0, '', ' ')?></td>
        <td style="text-align:left;width:150px;"><?php echo getFournisseur($row->id_fournisseur)["raison_social"] ?> </td>
        <td style="text-align:left;width:150px;"><?php echo $row->prix?></td>
    <!--<td><?php echo getTypemonnaie($row->id_devise)?></td>-->
    <td style="text-align:left;width:150px;">
      <?php 
              /*$d = $row->date_add;
              $dat =explode('/', $d);
              $date = $dat['1'].'/'.$dat['0'].'/'.$dat['2'];
              echo $date;*/
              echo $row->date_add;
        
      ?>
    </td>
     
    </tr>

    <?php 
    $qt_Total += (int)$row->qte;
    $cpt++;
    }
    ?>
    
      </tbody>
  </table>
  
    <h4>Qt Total : <?php echo number_format($qt_Total, 0, '', ' ');?></h4>
  
  </div>
  </div>
  <input class="btn btn-primary btn-small" type="button" value="Imprimer" onclick="javascript:printDiv('printablediv')" style="padding: 6px 24px; margin-top: 20px;font-weight: bold;" />
  <?php
    include("../template/footer-ajax.php") ;
    include("../template/footer-ajax.php");
  ?>
  
  <script type="text/javascript">
          $.fn.dataTableExt.sErrMode = 'throw'; 
          $(".tablesorter").tablesorter(); 
  </script>